package com.javer.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.javer.domain.KeyVal;
import com.javer.domain.PageView;
import com.javer.service.KeyValService;
import com.javer.util.WebUtil;

@Controller("/keyval")
public class KeyValAction extends DispatchAction {
	final Log logger = LogFactory.getLog(getClass());
	@Autowired private KeyValService keyValService;

	public ActionForward searchByKeyword(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String keyword = req.getParameter("keyword");
		PageView<KeyVal> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", keyValService.getPageByKeyword(pv, keyword));
		return new ActionForward("/manage/keyval/findresult.jsp");
	}
	
	public ActionForward getKeyValByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<KeyVal> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", keyValService.getByPage(pv));
		return mapping.findForward("list-page");
	}
	
	public ActionForward getKeyValById(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int id = Integer.valueOf(req.getParameter("id"));
		req.setAttribute("keyval", keyValService.getById(id));
		return mapping.findForward("form-page");
	}
	
}
